package base.utils;

import sun.rmi.runtime.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

public class GetApksSingnKeys {

    // 爱小艺学生
    private final static String studentAXYmd5SignerKeys = "4b3089c16f812ee57564b8ff5fde2b77";

    // 爱小艺教师
    private final static String teacherAXYmd5SignerKeys = "17698acab8b07706572e6641391e8a24";
//    private final static String teacherAXYmd5SignerKeys = "111111111111111111111";

    // 皓乐团
    private final static String groupmd5SignerKeys = "56a1512d6aa56a11f6735c9b2e1c86b5";


    public static void main(String[] args) {

        String apkUrl1 = "D:\\apks\\temp\\studentAXY33200_RP0522S.apk";
        String apkUrl2 = "D:\\apks\\temp\\teacherAXY33200_RP0522S.apk";
//        String apkUrl2 = "D:\\apks\\temp\\teacherGroup13300PRS.apk";



        ArrayList<String> apkPaths = new ArrayList<>();
        apkPaths.add(apkUrl1);
        apkPaths.add(apkUrl2);


        for (String item : apkPaths){
            String result = getApkSignerKeys(item);
            boolean isContain1 = checkSigerKeys(result);

            if (isContain1){
                String keysTypes = getSigerKeysType(result);
                System.out.println(item + " ; "+keysTypes +" ; " +result);
            }else {
                PrintUtils.print(item + "-----------------------------不存在此签名------------------------------");
                PrintUtils.print(item + "-----------------------------不存在此签名------------------------------");
                PrintUtils.print(item + "-----------------------------不存在此签名------------------------------");
                PrintUtils.print(item + "-----------------------------不存在此签名------------------------------");
                PrintUtils.print(item + "-----------------------------不存在此签名------------------------------");
                PrintUtils.print(item + "-----------------------------不存在此签名------------------------------");
            }

        }




////        String result = getSingle(apkUrl);
////        String result2 = getSingle(apkUrl1);
//
//
//        String result2 = getApkSignerKeys(apkUrl1);
//
//
//        boolean isContain1 = checkSigerKeys(result);
//        boolean isContain2 = checkSigerKeys(result2);
//
//        if (!isContain1){
//            PrintUtils.print(apkUrl + "--------------------不报行此签名-----------------------------");
//        }else {
//            System.out.println(apkUrl + " md5 key = " +result);
//        }
//
//
//        if (!isContain2){
//            PrintUtils.print(apkUrl1 + "--------------------不报行此签名-----------------------------");
//        }else {
//            System.out.println(apkUrl1 + " md5 key = " + result2);
//        }
//

    }








    public static String getSingle(String apkUrl) {
        Process p;
        //test.bat中的命令是ipconfig/all
        String cmd = "jarsigner -verify -verbose -certs " + apkUrl;
//        String cmd="jarsigner -verify -verbose -certs C:\\Users\\Administrator\\Desktop\\PandaClient.apk";
        String resultstr = null;

        try {
            //执行命令
            p = Runtime.getRuntime().exec(cmd);
            //取得命令结果的输出流
            InputStream fis = p.getInputStream();
            //用一个读输出流类去读
            //用缓冲器读行
            BufferedReader br = new BufferedReader(new InputStreamReader(fis, "GB2312"));
            String line = null;

            //直到读完为止
            int i = 0;
            while ((line = br.readLine()) != null) {
                if (line.contains("X.509")) {  //解析符合自己需要的內容，获取之后，直接返回。
                    System.out.println(line);
                    resultstr = line;
                    break;
                }
                i++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resultstr.replace("      X.509, ", "");
    }

    public static String getApkSignerKeys(String apkUrl) {

        Process p;
        //test.bat中的命令是ipconfig/all
//        String cmd = "jarsigner -verify -verbose -certs " + apkUrl;

        String apksigerJar = "D:\\tool\\sdk\\build-tools\\27.0.3\\lib\\apksigner.jar";

        String cmd = "java -jar " + apksigerJar + " verify --verbose --print-certs " + apkUrl;
        String resultstr = null;

//        System.out.println("cmd = " + cmd);

        try {
            //执行命令
            p = Runtime.getRuntime().exec(cmd);
            //取得命令结果的输出流
            InputStream fis = p.getInputStream();
            //用一个读输出流类去读
            //用缓冲器读行
            BufferedReader br = new BufferedReader(new InputStreamReader(fis, "GB2312"));
            String line = null;

            //直到读完为止
            int i = 0;
            while ((line = br.readLine()) != null) {
                if (line.contains("certificate MD5 digest")) {  //解析符合自己需要的內容，获取之后，直接返回。
                    resultstr = line;
//                    break;
                }
//                System.out.println(line);
                i++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("-------------------------------");
        assert resultstr != null;
        resultstr = resultstr.replace("Signer" , " ");
        resultstr = resultstr.replace("#1" , " ");
        resultstr = resultstr.replace("certificate" , " ");
        resultstr = resultstr.replace("MD5 digest:" , " ");
//        PrintUtils.print(" result = " + resultstr);
        return resultstr.trim();

    }

    public static boolean checkSigerKeys(String sigerKeyStr){

        boolean isContainKeys = false;

        ArrayList<String> keylist = new ArrayList<String>();
        keylist.add(studentAXYmd5SignerKeys);
        keylist.add(teacherAXYmd5SignerKeys);
        keylist.add(groupmd5SignerKeys);


        for (String keyStr : keylist){
            if (sigerKeyStr.contains(keyStr)){
                isContainKeys = true;
                break;
            }
        }



//        HashMap<String , String> keyMaps = new HashMap<>();
//        keyMaps.put(studentAXYmd5SignerKeys , "studentAXY");
//        keyMaps.put(teacherAXYmd5SignerKeys , "teahcerAXY");
//        keyMaps.put(groupmd5SignerKeys , "groupKeys");
//        boolean isContainKeys = keyMaps.containsKey(sigerKeyStr);
//        if (isContainKeys){
//
//        }else {
//
//
//        }

        return isContainKeys;

    }


  public static String getSigerKeysType(String sigerKeyStr){
        String apkType = null;

        HashMap<String , String> keyMaps = new HashMap<>();

        keyMaps.put(studentAXYmd5SignerKeys , "爱小艺学生");
        keyMaps.put(teacherAXYmd5SignerKeys , "爱小艺教师");
        keyMaps.put(groupmd5SignerKeys , "皓乐团key");

        boolean isContainKeys = keyMaps.containsKey(sigerKeyStr);

        if (isContainKeys){
            apkType = keyMaps.get(sigerKeyStr);
        }else {
            apkType = null;
        }
        return apkType;
    }




}

/*

D:\apks\temp\studentAXY33200_RP0522S.apk md5 key = null Signer #1 certificate MD5 digest: 4b3089c16f812ee57564b8ff5fde2b77     c4fcb400e229e979fadc4394492d7d8a
cmd = java -jar D:\tool\sdk\build-tools\27.0.3\lib\apksigner.jar verify --verbose --print-certs D:\apks\temp\teacherGroup13300PRS.apk


Verifies
Verified using v1 scheme (JAR signing): false
Verified using v2 scheme (APK Signature Scheme v2): true
Number of signers: 1
Signer #1 certificate DN: CN=holy, OU=holy, O=holy, L=holy, ST=holy, C=holy
Signer #1 certificate SHA-256 digest: 09cfee987b2a1cdc75aff4bdc88b4ceac52572d72a06c07f3b2aac1aeec32dd6
Signer #1 certificate SHA-1 digest: 089d95addcafe5ce063da2a098b4348dde6fe394

Signer #1 certificate MD5 digest: 56a1512d6aa56a11f6735c9b2e1c86b5


Signer #1 key algorithm: RSA
Signer #1 key size (bits): 2048
Signer #1 public key SHA-256 digest: 51c8d7d260c56cde43b569de30c8df88d44f2a72b30bd693d3b261e2da1f1ee2
Signer #1 public key SHA-1 digest: 2ec252cce64f2ba1743a9f4d4d374fc91b396204
Signer #1 public key MD5 digest: 77b116b7add23b3aaa50fac590c4e8c9




WARNING: APK Signature Scheme v2 signer #1: Unknown signature algorithm: 0x421


 */
